package dev.guide.net;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import dev.guide.R;

public class NetworkActivity extends Activity implements OnClickListener {
	private static final String TAG = "NetworkActivity";
	private BroadcastReceiver mUnmountReceiver = null;
	private TextView mLoginfo = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network);
		findViewById(R.id.button_check_connectivity).setOnClickListener(this);
		findViewById(R.id.multi_thread_download).setOnClickListener(this);

		mLoginfo = (TextView) findViewById(R.id.textview_loginfo);

		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(mNetChangeReceiver, filter);

		registerExternalStorageListener();
	}

	public void registerExternalStorageListener() {
		if (mUnmountReceiver == null) {
			mUnmountReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					String action = intent.getAction();
					Log.v(TAG,"action="+action);
					if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
					} else if (action.equals(Intent.ACTION_MEDIA_REMOVED)
							|| action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
					} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
						
					}
				}
			};
			IntentFilter iFilter = new IntentFilter();
			iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
			iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
			iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
			iFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
//			iFilter.addDataScheme("file");
			registerReceiver(mUnmountReceiver, iFilter);
		}
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(mNetChangeReceiver);
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_check_connectivity:
			checkConnectivity();
			break;
		case R.id.multi_thread_download:
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), MulThreadDownloader.class);
			startActivity(intent);
			break;
		}
	}

	private BroadcastReceiver mNetChangeReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			final String action = intent.getAction();
			if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
				NetworkActivity.this.checkConnectivity();
			}
		}
	};

	private void checkConnectivity() {
		ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (null == connManager) {
			mLoginfo.setText("Error! Can't get ConnectivityManager.");
			return;
		}

		NetworkInfo netInfo = connManager.getActiveNetworkInfo();
		if (null == netInfo) {
			mLoginfo.setText("Sorrry! getActiveNetworkInfo() is null.There is no active network.");
			return;
		}

		int netType = netInfo.getType();// netSubType = netInfo.getSubtype();
		if (netType == ConnectivityManager.TYPE_WIFI) {
			mLoginfo.setText("Type:" + netInfo.getTypeName() + "\nConnected:"
					+ netInfo.isConnected());
		} else if (netType == ConnectivityManager.TYPE_MOBILE) {
			if (netInfo.isRoaming()) {
				mLoginfo.setText("Warning! The mobile phone is Roaming."
						+ "\nType:" + netInfo.getTypeName() + "\nConnected:"
						+ netInfo.isConnected());
			} else {
				mLoginfo.setText("Type:" + netInfo.getTypeName()
						+ "\nNetSubType." + netInfo.getSubtypeName()
						+ "\nConnected:" + netInfo.isConnected());
			}
		} else {
			mLoginfo.setText("Network is unavailable!");
		}
	}

}
